<template>
    <div class="app-container">
        <el-card>
            <FormTopBar
                :title="title"
                :flowInsId="form.flowInsId"
                :taskId="taskId"
                :businessId="form.id"
                :billType="billType"
                :readonly="readonly"
                @submitForm="submitForm"
                @submitFormAndSaveAgain="submitFormAndSaveAgain"
            ></FormTopBar>
        </el-card>
        <!-- 添加或修改员工入职对话框 -->
        <el-form
            ref="form"
            :model="form"
            :rules="rules"
            label-width="150px"
            :disabled="readonly"
        >
            <el-card class="box-card top15" style="overflow: visible">
                <div slot="header" class="clearfix">
                    <span>基础信息</span>
                </div>
                <el-row>
                    <el-col :xs="18" :sm="18" :md="18" :lg="18" :xl="18">
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="姓名" prop="realName">
                                <el-input
                                    :disabled="
                                        this.$store.getters.allDefectObj[
                                            'HR-WEB-036'
                                        ] == 1
                                    "
                                    v-model="form.realName"
                                    placeholder=""
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="性别" prop="sex">
                                <el-select
                                    v-model="form.sex"
                                    placeholder="请选择"
                                >
                                    <el-option
                                        v-for="dict in dict.type.sys_user_sex"
                                        :key="dict.value"
                                        :label="dict.label"
                                        :value="dict.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="民族" prop="nation">
                                <el-select
                                    v-model="form.nation"
                                    placeholder="请选择"
                                >
                                    <el-option
                                        v-for="dict in dict.type.nation_list"
                                        :key="dict.value"
                                        :label="dict.label"
                                        :value="dict.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="身份证号" prop="idNum">
                                <el-input
                                    v-model="form.idNum"
                                    @blur="setBirthdate"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="出生日期" prop="birthdate">
                                <el-date-picker
                                    clearable
                                    v-model="form.birthdate"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                ></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="年龄" prop="age">
                                <el-input v-model="form.age"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="政治面貌" prop="political">
                                <el-select
                                    v-model="form.political"
                                    placeholder="请选择"
                                >
                                    <el-option
                                        v-for="dict in dict.type
                                            .political_status"
                                        :key="dict.value"
                                        :label="dict.label"
                                        :value="dict.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>

                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item
                                label="入党(团)时间"
                                prop="joinpartyDate"
                                :rules="[
                                    {
                                        validator: compareTime,
                                        message:
                                            '入党（团）时间必须大于出生日期',
                                        trigger: 'change',
                                    },
                                ]"
                            >
                                <el-date-picker
                                    clearable
                                    v-model="form.joinpartyDate"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                ></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="籍贯" prop="nativePlace">
                                <el-cascader
                                    :options="options"
                                    placeholder="省/地市/区"
                                    style="width: 100%"
                                    :props="{
                                        value: 'name',
                                        label: 'name',
                                        children: 'areas',
                                    }"
                                    v-model="areaArr"
                                    @change="setAreaForm"
                                >
                                </el-cascader>
                            </el-form-item>
                        </el-col>

                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="手机号" prop="phone">
                                <el-input v-model="form.phone" />
                            </el-form-item>
                        </el-col>

                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="电子邮箱" prop="mail">
                                <el-input v-model="form.mail" placeholder="" />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="联系人" prop="urgentContact">
                                <el-input
                                    v-model="form.urgentContact"
                                    placeholder=""
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item
                                label="联系人电话"
                                prop="urgentContactPhone"
                            >
                                <el-input
                                    v-model="form.urgentContactPhone"
                                    placeholder=""
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="家庭住址" prop="homeAddress">
                                <el-input
                                    v-model="form.homeAddress"
                                    placeholder=""
                                />
                            </el-form-item>
                        </el-col>
                    </el-col>
                    <el-col :xs="6" :sm="6" :md="6" :lg="6" :xl="6">
                        <el-form-item
                            label="员工头像"
                            style="margin-right: 10px"
                        >
                            <div class="text-center">
                                <userAvatar
                                    :user="user"
                                    :userId="form.userId"
                                />
                            </div>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-card>
            <el-card class="box-card top15" style="overflow: visible">
                <div slot="header" class="clearfix">
                    <span>拓展信息</span>
                </div>
                <el-row>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="员工状态" prop="staffType">
                                <el-select
                                    v-model="form.staffType"
                                    placeholder=""
                                >
                                    <el-option
                                        v-for="dict in dict.type.hrm_staff_type"
                                        :key="dict.value"
                                        :label="dict.label"
                                        :value="dict.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col
                            v-show="form.staffType == 0"
                            :xs="12"
                            :sm="12"
                            :md="12"
                            :lg="12"
                            :xl="12"
                        >
                            <el-form-item
                                label="实习时长"
                                prop="probationPeriod"
                            >
                                <el-select
                                    v-model="form.probationPeriod"
                                    placeholder=""
                                    clearable
                                >
                                    <el-option
                                        v-for="dict in internshipDurationDict"
                                        :key="dict.value"
                                        :label="dict.label"
                                        :value="dict.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>

                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="工号" prop="staffNum">
                                <el-input disabled v-model="form.staffNum" />
                            </el-form-item>
                        </el-col>

                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="部门" prop="deptId">
                                <treeselect
                                    v-model="form.deptId"
                                    :options="deptOptions"
                                    :show-count="true"
                                    placeholder=""
                                    :disabled="readonly"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="岗位类别" prop="postTypeCode">
                                <el-select
                                    v-model="form.postTypeCode"
                                    placeholder="请选择"
                                    @change="getPostList()"
                                >
                                    <el-option
                                        v-for="dict in dict.type.job_category"
                                        :key="dict.value"
                                        :label="dict.label"
                                        :value="dict.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>

                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="岗位名称" prop="postId">
                                <el-select
                                    v-model="form.postId"
                                    placeholder="请选择"
                                >
                                    <el-option
                                        v-for="item in postList"
                                        :key="item.postId"
                                        :label="item.postName"
                                        :value="item.postId"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>

                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="入职日期" prop="entryTime">
                                <el-date-picker
                                    clearable
                                    v-model="form.entryTime"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder=""
                                ></el-date-picker>
                            </el-form-item>
                        </el-col>

                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="备注" prop="remark">
                                <el-input
                                    type="textarea"
                                    :rows="3"
                                    placeholder=""
                                    v-model="form.remark"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>
                    </el-col>
                </el-row>
            </el-card>
            <el-card class="box-card top15" style="overflow: visible">
                <div slot="header" class="clearfix">
                    <span>教育经历</span>
                </div>
                <el-row>
                    <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="教育经历" prop="education">
                            <el-select v-model="form.education" placeholder="">
                                <el-option
                                    v-for="dict in dict.type.education_list"
                                    :key="dict.value"
                                    :label="dict.label"
                                    :value="dict.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item
                            label="教育经历取得时间"
                            prop="exGetXlDate"
                        >
                            <el-date-picker
                                clearable
                                v-model="form.exGetXlDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder=""
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="毕业院校" prop="exSchool">
                            <el-input v-model="form.exSchool" placeholder="" />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="专业" prop="exZy" width="150">
                            <el-input v-model="form.exZy" placeholder="" />
                        </el-form-item>
                    </el-col>

                    <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="学制（教育经历）" prop="exXuezhi">
                            <el-select v-model="form.exXuezhi" placeholder="">
                                <el-option
                                    v-for="dict in dict.type.educational_system"
                                    :key="dict.value"
                                    :label="dict.label"
                                    :value="dict.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="最高学位" prop="degree">
                            <el-select v-model="form.degree" placeholder="">
                                <el-option
                                    v-for="dict in dict.type.degree_value"
                                    :key="dict.value"
                                    :label="dict.label"
                                    :value="dict.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item
                            label="最高学位取得时间"
                            prop="exGetXwDate"
                        >
                            <el-date-picker
                                clearable
                                v-model="form.exGetXwDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder=""
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-card>
            <el-card class="box-card top15" style="overflow: visible">
                <div slot="header" class="clearfix">
                    <span>工作经历</span>
                    <el-button
                        style="float: right; padding: 3px 0"
                        type="text"
                        @click="handleAddWorkExp"
                    >
                        添加工作经历
                    </el-button>
                </div>
                <div
                    v-for="(item, index) in staffInductionWorkexpList"
                    :key="index"
                >
                    <el-divider v-if="index > 0" content-position="left"
                        >工作经历{{ index + 1 }}</el-divider
                    >
                    <el-row>
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item
                                :label="'公司名称'"
                                :prop="
                                    'staffInductionWorkexpList.' +
                                    index +
                                    '.companyName'
                                "
                                :rules="[
                                    {
                                        required: true,
                                        message: '公司名称不能为空',
                                        trigger: 'blur',
                                    },
                                ]"
                            >
                                <el-input
                                    v-model="item.companyName"
                                    placeholder="请输入公司名称"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item
                                :label="'公司地址'"
                                :prop="
                                    'staffInductionWorkexpList.' +
                                    index +
                                    '.companyAddress'
                                "
                                :rules="[
                                    {
                                        required: true,
                                        message: '公司地址不能为空',
                                        trigger: 'blur',
                                    },
                                ]"
                            >
                                <el-input
                                    v-model="item.companyAddress"
                                    placeholder="请输入公司地址"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item
                                :label="'所在部门'"
                                :prop="
                                    'staffInductionWorkexpList.' +
                                    index +
                                    '.companyDept'
                                "
                                :rules="[
                                    {
                                        required: true,
                                        message: '所在部门不能为空',
                                        trigger: 'blur',
                                    },
                                ]"
                            >
                                <el-input
                                    v-model="item.companyDept"
                                    placeholder="请输入所在部门"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item
                                :label="'职位'"
                                :prop="
                                    'staffInductionWorkexpList.' +
                                    index +
                                    '.position'
                                "
                                :rules="[
                                    {
                                        required: true,
                                        message: '职位不能为空',
                                        trigger: 'blur',
                                    },
                                ]"
                            >
                                <el-input
                                    v-model="item.position"
                                    placeholder="请输入职位"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item
                                :label="'证明人'"
                                :prop="
                                    'staffInductionWorkexpList.' +
                                    index +
                                    '.witness'
                                "
                                :rules="[
                                    {
                                        required: true,
                                        message: '证明人不能为空',
                                        trigger: 'blur',
                                    },
                                ]"
                            >
                                <el-input
                                    v-model="item.witness"
                                    placeholder="请输入证明人"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item
                                :label="'证明人联系方式'"
                                :prop="
                                    'staffInductionWorkexpList.' +
                                    index +
                                    '.witnessPhone'
                                "
                                :rules="[
                                    {
                                        required: true,
                                        message: '证明人联系方式不能为空',
                                        trigger: 'blur',
                                    },
                                    {
                                        pattern: /^1[3456789]\d{9}$/,
                                        message: '请输入正确的手机号',
                                        trigger: 'blur',
                                    },
                                ]"
                            >
                                <el-input
                                    v-model="item.witnessPhone"
                                    placeholder="请输入证明人联系方式"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item
                                :label="'工作开始时间'"
                                :prop="
                                    'staffInductionWorkexpList.' +
                                    index +
                                    '.startTime'
                                "
                                :rules="[
                                    {
                                        required: true,
                                        message: '工作开始时间不能为空',
                                        trigger: 'blur',
                                    },
                                ]"
                            >
                                <el-date-picker
                                    v-model="item.startTime"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="选择开始时间"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item
                                :label="'工作结束时间'"
                                :prop="
                                    'staffInductionWorkexpList.' +
                                    index +
                                    '.endTime'
                                "
                                :rules="[
                                    {
                                        required: true,
                                        message: '工作结束时间不能为空',
                                        trigger: 'blur',
                                    },
                                    {
                                        validator: (rule, value, callback) => {
                                            if (
                                                value &&
                                                item.startTime &&
                                                value < item.startTime
                                            ) {
                                                callback(
                                                    new Error(
                                                        '结束时间必须大于开始时间'
                                                    )
                                                );
                                            } else {
                                                callback();
                                            }
                                        },
                                        trigger: 'change',
                                    },
                                ]"
                            >
                                <el-date-picker
                                    v-model="item.endTime"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="选择结束时间"
                                />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-button
                        v-if="staffInductionWorkexpList.length > 0"
                        type="text"
                        @click="handleDeleteWorkExp(index)"
                        style="margin-left: 10px"
                    >
                        删除该工作经历
                    </el-button>
                </div>
            </el-card>
        </el-form>
    </div>
</template>

<script>
import {
    listInduction,
    getInduction,
    delInduction,
    addInduction,
    updateInduction,
} from "@/api/hrm/staff/induction";
import { getUserProfile } from "@/api/system/user";
import BillType from "@/billType";
import FlowInfo from "@/views/workflow/flow/flowInfo";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
// import { listPost } from "@/api/system/post";
import { listPost } from "@/api/system/post";
import { queryAll } from "@/api/area";
import userAvatar from "./userAvatar";

import FormTopBar from "@/views/workflow/common/formTopBar";
// 地区文件
import pcas from "@/assets/pcas-code.json";
const defaultForm = {
    sex: "0",
    staffType: "0",
};
export default {
    name: "StaffInductionForm",
    dicts: [
        "political_status",
        "education_list",
        "sys_yes_no",
        "family_relation_type",
        "nation_list",
        "marry_status",
        "language_level",
        "sys_user_sex",
        "driverslicense_type",
        "hrm_staff_type",
        "job_category",
        "educational_system",
        "degree_value",
    ],

    components: { FlowInfo, Treeselect, FormTopBar, userAvatar },
    data() {
        return {
            showFlowInfo: false,
            taskId: "",
            showCommitDialog: false,
            commitType: "",
            billType: BillType.billType.STAFF_INDUCTION,
            // 部门树选项
            deptOptions: [],
            // 职位----选择的职位
            postIds: [],
            //选择的驾照类型
            driverslicenseList: [],
            // 职位选项
            postList: [],
            // 子表选中数据
            checkedPlStaffInductionEduexp: [],
            // 子表选中数据
            checkedPlStaffInductionFamily: [],
            // 子表选中数据
            checkedPlStaffInductionWorkexp: [],
            // 员工入职表格数据
            inductionList: [],
            // 员工入职信息登记--教育经历表格数据
            staffInductionEduexpList: [],
            // 员工入职信息登记--家庭成员表格数据
            staffInductionFamilyList: [],
            // 员工入职信息登记--工作经历表格数据
            staffInductionWorkexpList: [],
            user: {},
            // 表单参数
            form: {
                ...defaultForm,
            },
            // 表单校验
            rules: {
                schoolName: [
                    {
                        required: true,
                        message: "毕业院校不能为空",
                        trigger: "blur",
                    },
                    {
                        max: 32,
                        message: "毕业院校长度不超过32个字符",
                        trigger: "blur",
                    },
                ],
                remark: [
                    {
                        max: 60,
                        message: "备注长度不超过60个字符",
                        trigger: "blur",
                    },
                ],
                postTypeCode: [
                    {
                        required: true,
                        message: "请选择岗位类别",
                        trigger: "change",
                    },
                ],
                wageNumber: [
                    {
                        required: true,
                        message: "工资编号不能为空",
                        trigger: "blur",
                    },
                    {
                        max: 32,
                        message: "工资编号长度不超过32个字符",
                        trigger: "blur",
                    },
                ],
                staffNum: [
                    {
                        required: true,
                        message: "工号不能为空",
                        trigger: "blur",
                    },
                    {
                        max: 32,
                        message: "工号长度不超过32个字符",
                        trigger: "blur",
                    },
                ],
                homeAddress: [
                    {
                        max: 50,
                        message: "家庭住址长度不超过50个字符",
                        trigger: "blur",
                    },
                ],
                realName: [
                    {
                        required: true,
                        message: "姓名不能为空",
                        trigger: "blur",
                    },
                    {
                        max: 50,
                        message: "姓名长度不超过50个字符",
                        trigger: "blur",
                    },
                ],
                deptId: [
                    {
                        required: true,
                        message: "归属部门为空",
                        trigger: "change",
                    },
                ],
                political: [
                    {
                        required: true,
                        message: "政治面貌不能为空",
                        trigger: "change",
                    },
                ],
                sex: [
                    {
                        required: true,
                        message: "性别不能为空",
                        trigger: "blur",
                    },
                ],
                education: [
                    {
                        required: true,
                        message: "请选择教育经历",
                        trigger: "change",
                    },
                ],
                height: [
                    {
                        type: "number",
                        message: "请输入数字（单位厘米cm）",
                        trigger: "blur",
                        transform: (value) => {
                            value = (value + "").replace(/(^\s*)|(\s*$)/g, ""); // 先去除前后空格
                            return value || value === 0 ? Number(value) : 0; // 转为数字
                        },
                    },
                ],
                weight: [
                    {
                        type: "number",
                        message: "请输入数字（单位公斤kg）",
                        trigger: "blur",
                        transform: (value) => {
                            value = (value + "").replace(/(^\s*)|(\s*$)/g, ""); // 先去除前后空格
                            return value || value === 0 ? Number(value) : 0; // 转为数字
                        },
                    },
                ],
                nation: [
                    {
                        required: true,
                        message: "民族不能为空",
                        trigger: "change",
                    },
                ],
                birthdate: [
                    {
                        required: true,
                        message: "出生日期不能为空",
                        trigger: "blur",
                    },
                ],
                entryTime: [
                    {
                        required: true,
                        message: "入职日期不能为空",
                        trigger: "blur",
                    },
                ],
                political: [
                    {
                        required: true,
                        message: "政治面貌不能为空",
                        trigger: "blur",
                    },
                ],
                // marryStatus: [
                //     {required: true, message: "婚姻状况不能为空", trigger: "blur"}
                // ],
                // nativePlace: [
                //     {required: true, message: "籍贯不能为空", trigger: "blur"}
                // ],
                idNum: [
                    {
                        required: true,
                        message: "身份证号不能为空",
                        trigger: "blur",
                    },
                    //身份证号的校验
                    {
                        pattern: /(^\d{15}$)|(^\d{112}$)|(^\d{17}(\d|X|x)$)/,
                        message: "身份证号格式不正确",
                        trigger: "blur",
                    },
                ],
                phone: [
                    {
                        required: true,
                        message: "手机号不能为空",
                        trigger: "blur",
                    },
                    {
                        pattern: /^1[3456789]\d{9}$/,
                        message: "请输入正确的手机号",
                        trigger: "blur",
                    },
                ],
                urgentContactPhone: [
                    {
                        max: 20,
                        message: "联系人电话不超过20个字符",
                        trigger: "blur",
                    },
                ],
                mail: [
                    {
                        max: 20,
                        message: "邮箱长度不超过20个字符",
                        trigger: "blur",
                    },
                ],
                // nowAddress: [
                //     {required: true, message: "现居住地不能为空", trigger: "blur"}
                // ],
                urgentContact: [
                    {
                        max: 20,
                        message: "联系人字符长度不超过20个字符",
                        trigger: "blur",
                    },
                ],
                // urgentContactRel: [
                //     {required: true, message: "紧急联系人关系不能为空", trigger: "blur"}
                // ],
                // urgentContactAddress: [
                // { max: 50, message: "家庭住址字符长度不超过20个字符", trigger: "blur"},
                // ],
                // urgentContactPhone: [
                //     {required: true, message: "紧急联系人电话不能为空", trigger: "blur"}
                // ],
                staffType: [
                    {
                        required: true,
                        message: "编制类型不能为空",
                        trigger: "blur",
                    },
                ],
            },
            readonly: false,
            options: [],
            areaArr: [],
            internshipDurationDict: [
                {
                    label: "1个月",
                    value: 1,
                },
                {
                    label: "2个月",
                    value: 2,
                },
                {
                    label: "3个月",
                    value: 3,
                },
                {
                    label: "4个月",
                    value: 4,
                },
                {
                    label: "5个月",
                    value: 5,
                },

                {
                    label: "6个月",
                    value: 6,
                },
            ],
        };
    },
    provide() {
        return {
            addCcedList: this.addCcedList,
        };
    },
    computed: {
        title() {
            return this.readonly
                ? "查看员工信息"
                : this.$route.params.formId
                ? "编辑员工信息"
                : "录入员工信息";
        },
    },
    created() {
        //菜单传进来的参数
        const readonly = this.$route.query && this.$route.query.readonly;
        const formId = this.$route.params && this.$route.params.formId;
        const taskId = this.$route.query && this.$route.query.taskId;

        if (taskId) {
            this.taskId = taskId;
        }
        if (formId) {
            this.form.id = formId;
            this.getDetailData(formId);
        }
        console.log("readonly:" + readonly);
        if ("1" == readonly) {
            this.readonly = true;
        }

        // 查询省市区
        this.getAreaData();

        //查询基础数据
        this.getTreeselect();
        // this.getPostList();
    },
    mounted() {
        console.log(this.dict.type.driverslicense_type);
        this.getUser();
    },
    methods: {
        handleDeleteWorkExp(index) {
            this.$modal.confirm("确定删除该工作经历吗？").then(() => {
                this.staffInductionWorkexpList.splice(index, 1);
            });
        },

        handleAddWorkExp() {
            this.staffInductionWorkexpList.push({});
        },

        //比较两个时间先后
        compareDates(dateString1, dateString2) {
            // 尝试将字符串解析为Date对象
            const date1 = new Date(dateString1);
            const date2 = new Date(dateString2);

            // 检查是否解析成功（即不是Invalid Date）
            if (isNaN(date1.getTime()) || isNaN(date2.getTime())) {
                console.error("Invalid date string");
                return null; // 或者你可以抛出一个错误
            }

            // 比较日期
            if (date1 < date2) {
                return -1; // dateString1 在 dateString2 之前
            } else if (date1 > date2) {
                return 1; // dateString1 在 dateString2 之后
            } else {
                return 0; // 两个日期相同
            }
        },
        //入党团时间必须大于出生日期
        compareTime(rule, value, callback) {
            if (
                value != null &&
                this.form.birthdate != null &&
                this.compareDates(this.form.birthdate, value) != -1
            ) {
                callback(new Error("入党（团）时间必须大于出生日期"));
            } else {
                callback();
            }
        },
        getUser() {
            getUserProfile().then((response) => {
                this.user = response.data;
            });
        },
        getAreaData() {
            queryAll().then((response) => {
                this.options = response.data;
            });
        },
        /**
         * 设置市区街道数据
         */
        setAreaForm(areaArr) {
            console.log(areaArr, "areaArr");
            this.form.nativePlace = "";

            if (areaArr && areaArr.length > 0) {
                areaArr.forEach((areaName, index) => {
                    if (index == 0) {
                        this.form.nativePlace = areaName;
                    } else {
                        this.form.nativePlace =
                            this.form.nativePlace + "/" + areaName;
                    }
                });
            }
        },

        /**根据身份证号设置生日 */
        setBirthdate() {
            if (this.form.idNum) {
                /**从身份证号中解析出出生日期(2020-05-05)*/
                const year = this.form.idNum.substring(6, 10);
                const month = this.form.idNum.substring(10, 12);
                const day = this.form.idNum.substring(12, 14);
                this.$set(
                    this.form,
                    "birthdate",
                    year + "-" + month + "-" + day
                );
                /**从年龄=当前日期-出生日期=N年M月（M＜12），年龄=N*/
                const now = new Date();
                const year1 = now.getFullYear();
                const month1 = now.getMonth();
                const day1 = now.getDate();
                const age = year1 - year;
                if (month1 < month) {
                    age--;
                }
                this.$set(this.form, "age", age);
            }
        },

        /** 查询部门下拉树结构 */
        getTreeselect() {
            treeselect().then((response) => {
                this.deptOptions = response.data;
            });
        },
        getPostList() {
            //清空岗位名称

            this.postIds = [];
            listPost({
                pageSize: 100,
                postTypeCode: this.form.postTypeCode,
            }).then((res) => {
                this.postList = res.rows;
            });
        },
        //  获取单据信息
        getDetailData(id) {
            getInduction(id).then((response) => {
                this.form = response.data;
                this.form.billType = this.billType;
                //驾照类型
                if (
                    this.form.driverslicenseType &&
                    this.form.driverslicenseType.length > 0
                ) {
                    this.driverslicenseList =
                        this.form.driverslicenseType.split(",");
                }
                //岗位
                if (this.form.postTypeCode) {
                    this.getPostList();
                }
                console.log(this.form.postId);
                console.log(this.form.postId && this.form.postId.length > 0);

                if (this.form.nativePlace) {
                    let areaArr = this.form.nativePlace.split("/");
                    if (areaArr.length > 0) {
                        this.areaArr = areaArr;
                    }
                }

                this.form.postId = Number(this.form.postId);
                this.staffInductionEduexpList =
                    response.data.staffInductionEduexpList;
                this.staffInductionFamilyList =
                    response.data.staffInductionFamilyList;
                this.staffInductionWorkexpList =
                    response.data.staffInductionWorkexpList;
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (
                        this.driverslicenseList &&
                        this.driverslicenseList.length > 0
                    ) {
                        this.form.driverslicenseType =
                            this.driverslicenseList.join(",");
                    }

                    if (this.postIds && this.postIds.length > 0) {
                        this.form.postId = this.postIds.join(",");
                    }
                    this.form.staffInductionEduexpList =
                        this.staffInductionEduexpList;
                    this.form.staffInductionFamilyList =
                        this.staffInductionFamilyList;
                    this.form.staffInductionWorkexpList =
                        this.staffInductionWorkexpList;
                    if (this.form.id != null) {
                        updateInduction(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.closeThisPage();
                        });
                    } else {
                        addInduction(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.closeThisPage();
                        });
                    }
                }
            });
        },
        submitFormAndSaveAgain() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (
                        this.driverslicenseList &&
                        this.driverslicenseList.length > 0
                    ) {
                        this.form.driverslicenseType =
                            this.driverslicenseList.join(",");
                    }

                    if (this.postIds && this.postIds.length > 0) {
                        this.form.postId = this.postIds.join(",");
                    }
                    this.form.staffInductionEduexpList =
                        this.staffInductionEduexpList;
                    this.form.staffInductionFamilyList =
                        this.staffInductionFamilyList;
                    this.form.staffInductionWorkexpList =
                        this.staffInductionWorkexpList;
                    if (this.form.id != null) {
                        updateInduction(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.resetThisPage();
                        });
                    } else {
                        addInduction(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.resetThisPage();
                        });
                    }
                }
            });
        },

        closeThisPage() {
            // const obj = { path: "/hrm/staff/induction" };
            // this.$tab.closeOpenPage(obj);
            this.$router.go(-1);
        },
        resetThisPage() {
            this.form = { ...defaultForm };
            this.$refs["form"].resetFields();
        },
        /** 教育经历序号 */
        rowPlStaffInductionEduexpIndex({ row, rowIndex }) {
            row.index = rowIndex + 1;
        },
        /** 家庭成员序号 */
        rowPlStaffInductionFamilyIndex({ row, rowIndex }) {
            row.index = rowIndex + 1;
        },
        /** 工作经历序号 */
        rowPlStaffInductionWorkexpIndex({ row, rowIndex }) {
            row.index = rowIndex + 1;
        },
        /** 教育经历添加按钮操作 */
        handleAddPlStaffInductionEduexp() {
            let obj = {};
            obj.remark = "";
            obj.schoolName = "";
            obj.schoolAddress = "";
            obj.isAllday = "";
            obj.major = "";
            obj.education = "";
            obj.startDate = "";
            obj.endDate = "";
            this.staffInductionEduexpList.push(obj);
        },
        /** 家庭成员信息添加 */
        handleAddPlStaffInductionFamily() {
            let obj = {};
            obj.remark = "";
            obj.name = "";
            obj.birthdate = "";
            obj.age = "";
            obj.political = "";
            obj.company = "";
            obj.position = "";
            obj.phone = "";
            obj.relation = "";
            this.staffInductionFamilyList.push(obj);
        },
        /** 工作经历信息添加 */
        handleAddPlStaffInductionWorkexp() {
            let obj = {};
            obj.remark = "";
            obj.companyName = "";
            obj.companyAddress = "";
            obj.companyDept = "";
            obj.position = "";
            obj.witness = "";
            obj.witnessPhone = "";
            this.staffInductionWorkexpList.push(obj);
        },
        /** 教育经历删除按钮操作 */
        handleDeletePlStaffInductionEduexp() {
            if (this.checkedPlStaffInductionEduexp.length == 0) {
                this.$modal.msgError("请先选择要删除的教育经历信息");
            } else {
                const staffInductionEduexpList = this.staffInductionEduexpList;
                const checkedPlStaffInductionEduexp =
                    this.checkedPlStaffInductionEduexp;
                this.staffInductionEduexpList = staffInductionEduexpList.filter(
                    function (item) {
                        return (
                            checkedPlStaffInductionEduexp.indexOf(item.index) ==
                            -1
                        );
                    }
                );
            }
        },
        /** 家庭成员删除按钮操作 */
        handleDeletePlStaffInductionFamily() {
            if (this.checkedPlStaffInductionFamily.length == 0) {
                this.$modal.msgError("请先选择要删除的家庭成员信息");
            } else {
                const staffInductionFamilyList = this.staffInductionFamilyList;
                const checkedPlStaffInductionFamily =
                    this.checkedPlStaffInductionFamily;
                this.staffInductionFamilyList = staffInductionFamilyList.filter(
                    function (item) {
                        return (
                            checkedPlStaffInductionFamily.indexOf(item.index) ==
                            -1
                        );
                    }
                );
            }
        },
        /** 工作经历删除按钮操作 */
        handleDeletePlStaffInductionWorkexp() {
            if (this.checkedPlStaffInductionWorkexp.length == 0) {
                this.$modal.msgError("请先选择要删除的工作经历");
            } else {
                const staffInductionWorkexpList =
                    this.staffInductionWorkexpList;
                const checkedPlStaffInductionWorkexp =
                    this.checkedPlStaffInductionWorkexp;
                this.staffInductionWorkexpList =
                    staffInductionWorkexpList.filter(function (item) {
                        return (
                            checkedPlStaffInductionWorkexp.indexOf(
                                item.index
                            ) == -1
                        );
                    });
            }
        },
        /** 复选框选中数据 */
        handlePlStaffInductionEduexpSelectionChange(selection) {
            this.checkedPlStaffInductionEduexp = selection.map(
                (item) => item.index
            );
        },
        /** 复选框选中数据 */
        handlePlStaffInductionFamilySelectionChange(selection) {
            this.checkedPlStaffInductionFamily = selection.map(
                (item) => item.index
            );
        },
        /** 复选框选中数据 */
        handlePlStaffInductionWorkexpSelectionChange(selection) {
            this.checkedPlStaffInductionWorkexp = selection.map(
                (item) => item.index
            );
        },
        // 创建单据时候，选择了抄送人
        addCcedList(ccedList) {
            this.form.ccedList = ccedList;
            console.log("回填抄送人");
            console.log(this.form);
        },
    },
};
</script>
<style scoped>
/deep/.vue-treeselect__input-container input {
    display: block;
}
</style>
<style scoped lang="scss"></style>
